package MyFragment;

import android.content.Context;
import android.hardware.display.DisplayManager;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;

import com.qb.devtest.MyApplication;
import com.qb.devtest.R;

import CustomView.Custom2Screen;
import utils.MyUtil;

public class Fragment2Screen extends Fragment {
    Context ctx = MyApplication.getInstance();
    TextView tv2Screen;
    private DisplayManager displayManager;
    private Custom2Screen mPresentation;
    Boolean screen2IsInit = false;

    //收起软键盘
    public static void hideSoftInputFromWindow(Context ctx,EditText et) {
        InputMethodManager imm = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_2screen, container, false);
        tv2Screen = view.findViewById(R.id.tv_2screen);

        Button btOpen = view.findViewById(R.id.btn_2screen_open);
        Button btClose = view.findViewById(R.id.btn_2screen_close);

        btOpen.setEnabled(true);
        btClose.setEnabled(false);
        tv2Screen.setText("双屏异显已关闭");
        tv2Screen.setTextColor(getResources().getColor(R.color.red_start));

        btOpen.setOnClickListener(view1 -> {
            if (showSecondaryScreen()) {
                btOpen.setEnabled(false);
                btClose.setEnabled(true);
                tv2Screen.setText("双屏异显已开启");
                tv2Screen.setTextColor(getResources().getColor(R.color.teal_700));
            }
        });
        btClose.setOnClickListener(view1 -> {
            closeSecondaryScreen();
            btOpen.setEnabled(true);
            btClose.setEnabled(false);
            tv2Screen.setText("双屏异显已关闭");
            tv2Screen.setTextColor(getResources().getColor(R.color.red_start));
        });
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // 在这里初始化你的Fragment视图组件
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        closeSecondaryScreen();
    }

    Boolean showSecondaryScreen() {
        Display[] displays;//屏幕数组
        displayManager = (DisplayManager) ctx.getSystemService(Context.DISPLAY_SERVICE);
        displays = displayManager.getDisplays(); //得到显示器数组
        if (displays.length>1) {
            mPresentation = new Custom2Screen(ctx.getApplicationContext(),displays[1]);//displays[1]是副屏
            screen2IsInit = true;
//        mPresentation.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            mPresentation.show();
            return true;
        } else {
            tv2Screen.setText("当前设备只有一个屏幕");
            tv2Screen.setTextColor(getResources().getColor(R.color.red_start));
            return false;
        }
    }

    void closeSecondaryScreen() {
        if (screen2IsInit && mPresentation.isShowing()) mPresentation.cancel();
    }

}